package com.hz.supermanage.dao;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.dao.CommonIDU;
import com.hz.framework.dao.CommonSel;
import com.hz.framework.util.CommonUtils;
import com.hz.framework.util.PropertiesUtil;
import com.hz.supermanage.bean.*;

/**
 * ajax DWR方法类
 * */
public class HzsysAjaxDao {

	private List lstValue = null;
	private boolean flag = false;
	/** 组织人员角色权限管理 --------------------------------------------------------------------开始**/
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param map  对应 值的键值对
	 * @param names
	 * @param table
	 * @return 方法功能: 保存树形结构方法
	 */
	public boolean saveForm(Map map, String names, String table,
			HttpServletRequest request) {
		CommonDao cd= new CommonDao();
		Hzsysorganization dep = new Hzsysorganization();
		PropertiesUtil p = new PropertiesUtil();
		String PropertiesName = p.getProperties("server.id");
		String a = p.getMaxId("Hzsysorganization", "depid", PropertiesName);
		dep.setDepid(PropertiesName.concat(a));
		dep.setParentid((String) map.get("parentid"));
		dep.setDepname((String) map.get("depname"));
		dep.setDepnumber((String) map.get("depnumber"));
		dep.setDeporder((String) map.get("deporder"));
		dep.setDepcontent((String) map.get("depcontent"));
		dep.setDeptype((String) map.get("deptype"));
		dep.setDeplink((String) map.get("deplink"));
		dep.setDepstate("A");
		String zj = cd.saveReturn(dep);

		if ("-1".equals((String) map.get("parentid"))) {
			dep.setUrl(map.get("parentid") + "." + zj);
		} else {
			Hzsysorganization depz = (Hzsysorganization) cd.selHqlObj("from Hzsysorganization where depid='"
					+ map.get("parentid") + "'");
			dep.setUrl(depz.getUrl() + "." + zj);
		}
		dep.setDepid(zj);
		flag = cd.updateinfor(dep);
		// System.out.println("树插入方法");
		return flag;
	}
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param map  对应 值的键值对
	 * @param names
	 * @param table
	 * @return 方法功能: 更新树形结构信息
	 */
	public boolean updateForm(Map map, String names, String table) {
		CommonDao cd= new CommonDao();
		Hzsysorganization dep = (Hzsysorganization) cd.selHqlObj("from Hzsysorganization where depid ='"
				+ map.get("depid") + "'");
		dep.setUserid((String) map.get("userid"));
		dep.setDepname((String) map.get("depname"));
		dep.setDepnumber((String) map.get("depnumber"));
		dep.setDeporder((String) map.get("deporder"));
		dep.setDepcontent((String) map.get("depcontent"));
		dep.setDeplink((String) map.get("deplink"));
		flag = cd.updateinfor(dep);
		return flag;
	}
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param key  对应树结构id
	 * @param type  对应类别 posid 岗位, deptid 部门, orgid 单位
	 * @return 方法功能: 删除树结构，并且更新人员所属部门
	 */
	public String delTree(String key, String type) {
		CommonSel csel= new CommonSel();
		Hzsysorganization dep = (Hzsysorganization) csel.selHqlObj("from Hzsysorganization where depid ='" + key + "'");
		String pid = dep.getParentid();
		lstValue = csel.selHqlList("from Hzsysorganization where url like '%." + key + ".%'");
		String depid = "";
		for (Iterator iter = lstValue.iterator(); iter.hasNext();) {
			Hzsysorganization depz = (Hzsysorganization) iter.next();
			depid += "'" + depz.getDepid().toString() + "',";
		}
		flag = csel.DeleteHql("Hzsysorganization", "depid", depid + "'" + key + "'"); // 删除本节点和子节点,并删除子节点中的子节点

		if (type.equals("posid")) {
			String hql = "update Hzsyspersonnel set posid = null where posid = '"
					+ key + "'";
			flag = csel.transactHql(hql);
			System.out.println("岗位" + hql);
		}
		if (type.equals("deptid")) {
			String hql = "update Hzsyspersonnel set deptid=null,posid = null where deptid = '"
					+ key + "'";
			flag = csel.transactHql(hql);
			System.out.println("部门" + hql);
		}
		if (type.equals("orgid")) {
			String hql = "update Hzsyspersonnel set orgid='',deptid=null,posid = null where orgid = '"
					+ key + "'";
			flag = csel.transactHql(hql);
			System.out.println("单位" + hql);
		}
		System.out.println("树删除方法");
		return pid;
	}
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param fname 文件名
	 * @param fplace  文件中间地址
	 * @param request
	 * @return 方法功能:   为点选 树结构层 读写xml文件所用
	 */
	public String XmlReadValue(String fname, String fplace,
			HttpServletRequest request) {
		String xmlvalue = "";
		String path = request.getRealPath("\\"); // 获得当前文件夹路径
		String url = path + fplace + fname + ".xml";
		SAXBuilder builder = new SAXBuilder();
		InputStream file;
		try {
			file = new FileInputStream(url);
			Document document = builder.build(file);// 获得文档对象
			Element root = document.getRootElement();// 获得根节点
			xmlvalue = root.getValue();
			file.close();
			CommonUtils.deBug(xmlvalue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return xmlvalue;
	}
//	保存权限栏目表信息
	public boolean saveRole(Map map,String names,String table) {
		CommonSel sel= new CommonSel();
		//保存角色信息
		if(table.equals("HzsysRoleinfo")){
			HzsysRoleinfo roleinfo = new HzsysRoleinfo();
			roleinfo.setRolename((String) map.get("rolename"));
			roleinfo.setRoleorder((String) map.get("roleorder"));
			roleinfo.setRcontent((String) map.get("rcontent"));
			flag = sel.insertinfor(roleinfo);
		}else{
			//保存权限栏目信息
			HzsysPrograma pro = new HzsysPrograma();
			pro.setParentid(Integer.valueOf((String) map.get("parentid")));
			pro.setProname((String) map.get("proname"));
			//pro.setPronumber((String) map.get("pronumber"));
			pro.setProorder((String) map.get("proorder"));
			
			String protype = (String) map.get("protype");
			String procontent = (String) map.get("procontent");
			String prosoncontent = map.get("prosoncontent")==null?"":(String)map.get("prosoncontent");
			if(protype.equals("D") && "".equals(procontent)){
				procontent = "/docs/login/mainbody.jsp";
			}else if(protype.equals("O") && "".equals(procontent)) {
				procontent = "#";
			}
			pro.setProcontent(procontent);
			pro.setProsoncontent(prosoncontent);
			pro.setProtype(protype);
			pro.setProstate("A");
			String zj = sel.saveReturn(pro);
			
			if("-1".equals((String)map.get("parentid"))){
				pro.setUrl(map.get("parentid")+"."+zj);
			}else{
				HzsysPrograma proz = (HzsysPrograma) sel.selHqlObj("from HzsysPrograma where proid="+ map.get("parentid"));
				pro.setUrl(proz.getUrl()+"."+zj);
			}
			pro.setProid(Integer.valueOf(zj));
			flag = sel.updateinfor(pro);
		}
		
		return flag;
	}
//	更新权限栏目表信息
	public boolean updateRole(Map map,String names,String table) {
		CommonSel sel= new CommonSel();
		HzsysPrograma pro = (HzsysPrograma) sel.selHqlObj("from HzsysPrograma where proid ="+map.get("proid").toString());
		pro.setProname((String) map.get("proname"));
		//pro.setPronumber((String) map.get("pronumber"));
		pro.setProorder((String) map.get("proorder"));
		pro.setProcontent((String) map.get("procontent"));
		pro.setProsoncontent(map.get("prosoncontent")==null?"":(String)map.get("prosoncontent") );
		flag = sel.updateinfor(pro);
		return flag;
	}
	//删除权限表栏目信息
	public String  deleteRole(String key,String type) {
		CommonSel sel= new CommonSel();
		HzsysPrograma pro = (HzsysPrograma) sel.selHqlObj("from HzsysPrograma where proid ='"+key+"'");
		String pid=pro.getParentid().toString();
		lstValue = sel.selHqlList("from HzsysPrograma where url like '%."+key+".%'");
		String proid = "";
		for(Iterator iter = lstValue.iterator(); iter.hasNext();){
			HzsysPrograma proz = (HzsysPrograma) iter.next();
			proid += proz.getProid().toString() + ",";
		}
		flag = sel.DeleteHql("HzsysPrograma", "proid", proid + key); //删除本节点和子节点,并删除子节点中的子节点
		return pid;
	}
//	保存权限到角色  或 权限到用户
	public String SaveGramaRole(Map map,String GRflag) {
		String rflag="";
		String gramaid = (String) map.get("grama");
		String roleid = (String) map.get("roleid");
		if(!"".equals(gramaid)){
			gramaid = gramaid.replace("ROOT,", "");
		}
		CommonSel sel= new CommonSel();
		if(GRflag.equals("R")){
			if(!"".equals(roleid)){
				List rlist = sel.selHqlList("from HzsysRoleinfo where roleid in ("+roleid.substring(1, roleid.lastIndexOf(","))+")");
				if(!rlist.isEmpty()){
					List saver= new ArrayList();
					for(Iterator iter = rlist.iterator(); iter.hasNext();){
						HzsysRoleinfo ro = (HzsysRoleinfo)iter.next();
						ro.setRolegrama(gramaid);
						saver.add(ro);
					}
					flag = sel.saveOrupdate(saver);
				}
				rflag =  flag?"保存权限到角色成功!":"保存权限到角色错误!";
			}else{
				rflag = "保存权限到角色错误!";
			}
			
		}else if(GRflag.equals("P")){
			String perid = (String) map.get("perid");
			if(!"".equals(perid)){
				Hzsyspersonnel per = (Hzsyspersonnel)sel.selHqlObj("from Hzsyspersonnel where personid = '"+perid+"'");
				per.setGramaid(gramaid);
				per.setRoleid(roleid);
				flag = sel.updateinfor(per);
				rflag = flag?"保存角色权限到人员成功!":"保存角色权限到人员错误!";
			}else{
				rflag = "保存角色权限到人员错误!";
			}
		}
		return rflag;
	}
//	删除角色信息
	public boolean deleteInfo(String table,String where ,String value) {
		//截掉最后一个 ， 号
		CommonIDU idu= new CommonIDU();
		try{
			value = value.substring(value.indexOf(",")+1, value.lastIndexOf(","));
			flag = idu.DeleteHql(table, where, value); //删除本节点和子节点,并删除子节点中的子节点
		}catch (Exception e) {
			return flag;
		}
		return flag;
	}
//	删除客户信息及提醒信息
	public void DeleteEmpHql(String table, String where, String value){
		CommonIDU idu= new CommonIDU();
		idu.DeleteHql(table, where, value);//删除人员
		idu.DeleteHql("Empholdpost", "perid", value);//删除任职
		//idu.DeleteHql("Notice", "linkmanid", value);//删除提醒
	}
	/** 组织人员角色权限管理 ----------------------------------------------------------------------结束**/
	
}
